package com.macrolab.code.api.dictEnum

import com.macrolab.config.DataSourceConfig
import com.macrolab.util.GDAO
import com.macrolab.util.MyTools
import com.macrolab.util.VelocityUtil
import groovy.json.JsonSlurper
import org.apache.commons.io.FileUtils
import org.apache.groovy.json.internal.JsonFastParser

/**
 * 数据字典相关源码生成工具
 *
 * 生成 字典表中 字典条目值的枚举类
 *
 */

// ============= 模板文件 ==================
def templateFile = "DictEnum.java.vm"

// ============= 模板变量 ==================
def packageName = "com.ruoyi.enums.status"

// ============= 代码生成的路径 =============
def projPath = "D:\\my\\workspace\\genCode\\"
def outputPath = projPath + "output/enums/"

MyTools.createTmpDir(outputPath)
println "代码生成位置 ==> ${outputPath}"

// ============= 数据字典所在数据库 ==========
//GDAO dao = DataSourceConfig.getLocalDAO()
GDAO dao = DataSourceConfig.getRemoteDAO()


// ==================================================================================================================================
def f = new File(templateFile)

// 生成日志文件
//File outLog = new File("codeGeneratorLog4State.txt")
//outLog.newWriter('GBK')
//def msg = "生成数据字典枚举类文件：\n"
//outLog << msg

// 显示字典所有的条目
def dictTypeRows = dao.query("select * from sys_dict_type")
dictTypeRows.each { r ->
    def dictDataRows = dao.query("select * from sys_dict_data where dict_type='${r['dict_type']}'")

    println "字典条目【${r['dict_name']}】"

    String enumTemplate = ""
    int cnt = dictDataRows.size()
    def enums = [:]
    dictDataRows.each { row ->

        cnt--
//        def enumName = StrKit.isBlank(row['enum_name']) ? "V${row['code']}" : row['enum_name']
//        def enumName = "V${row['dict_value']}"

//        ${enum.enumName}(${enum.value},"${enum.label}");  // ${enum.notes}

        enums['value'] = row['dict_value']
        enums['label'] = row['dict_label']
        try {
            def obj = new JsonSlurper().parseText(row['remark'])
            enums['enumName'] = obj['enumName']
            enums['notes'] = obj['notes']
        } catch (Exception e) {
            enums['enumName'] = "V${row['dict_value']}"
            enums['notes'] = row['remark']
        }
        println "  ==> " + "词条值【${row['dict_value']}】".padRight(5, " ") + ":" + row['dict_label'].toString().padRight(10, " ") + "枚举变量： ${enums} "

//        enumTemplate += "     ${enumName}(${row['dict_value']}, \"${row['dict_label']}\")${cnt == 0 ? ";" : ","}  ${row['remark'] ? "// ${row['remark']}" : ""} \n"
    }
    def camelName = MyTools.buildCamelName(r['dict_type'], true)

    // 绑定模板文件、变量
    VelocityUtil velocity = new VelocityUtil();
    velocity.setInputEncoding("UTF-8")
    velocity.putObj("packageName", packageName)
    velocity.putObj("className", "Enum" + camelName)
    velocity.putObj("enums", enums)
    String content = velocity.mergeByVMFile(templateFile);

     File outUpdate = new File(outputPath + "${binding['className']}.java")
    outUpdate.newWriter('GBK')
    outUpdate << content
//    FileUtils.writeStringToFile(new File(outputPath + "${binding['className']}.java", content))
//    System.out.print(content);
}

msg = "代码生成完毕.  ${MyTools.date2String(new Date())} "
outLog << msg << "\n"; println msg

println "代码生成位置 ==> ${outputPath}"
